package com.asder.tool.generator.main;

/**
 * Created by zy on 2020/10/2.
 */

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;


/**
 * 程序入口
 */
public class HuaYuan {
    public static void main(String[] args) {
        JFrame window = new DrawFrame();
        window.setTitle("绘制图形");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setBounds(100, 100, 800, 800);
        window.setVisible(true);
    }
}

//窗口类
class DrawFrame extends JFrame {
    public DrawFrame() {
        add(new DrawComponent());
        pack();
    }
}

//图形类
class DrawComponent extends JComponent {
    private static final int DEAFULT_WIDTH = 800;
    private static final int DEFAULT_HEIGHT = 800;

    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        //绘制矩形
        double leftx = 250;
        double topy = 250;
        double width = 200;
        Rectangle2D rect = new Rectangle2D.Double(leftx, topy, width, width);
        g2.draw(rect);

//        //绘制椭圆
//        Ellipse2D ellipse = new Ellipse2D.Double();
//        ellipse.setFrame(rect);
//        g2.draw(ellipse);
//        //绘制直线
//        g2.draw(new Line2D.Double(leftx,topy,leftx + width ,topy + height));
        //外接圆
        double centerx = rect.getCenterX();
        double centery = rect.getCenterY();
        double radius = (width / 2) * squareTwo();
        Ellipse2D circle = new Ellipse2D.Double();
        circle.setFrameFromCenter(centerx, centery, centerx + radius, centery + radius);
        g2.draw(circle);

        // 外接方
        double width2 = width;
        double radius2 = radius;
        double leftx2 = leftx- (radius2 - (width2 / 2));

        for (int i = 0; i < 2; i++) {
            width2 = radius2 * 2;

            huaFang(g2, leftx2, width2);

            // 外接圆
            radius2 = radius2 * squareTwo();
            huaYan(g2, rect, radius2);

            leftx2 = leftx2 - (radius2 - (width2 / 2));
            width2 = radius2 * 2;
            huaFang(g2, leftx2, width2);
        }


                width = width / 2;
        for (int i = 0; i < 1; i++) {
            // 内接圆
            huaYan(g2, rect, width);
            // 内接方
            radius = width*squareTwo();
            leftx = leftx+ (width- radius/2);
            huaFang(g2, leftx, radius);
            width =  radius /2;
        }

    }

    public void huaYan(Graphics2D g2, Rectangle2D rect, double radius) {
        Ellipse2D circle2 = new Ellipse2D.Double();
        double centerx = rect.getCenterX();
        double centery = rect.getCenterY();
        circle2.setFrameFromCenter(centerx, centery, centerx + radius, centery + radius);
        g2.draw(circle2);
    }

    public void huaFang(Graphics2D g2, double leftx, double width) {

        Rectangle2D rect = new Rectangle2D.Double(leftx, leftx, width, width);
        g2.draw(rect);
    }

    public static double squareTwo() {
        final double frequency = 0.00000000001;
        double l = 1.4;
        double h = 1.5;
        while (h - l > frequency) {
            double mid = (h + l) / 2.0;
            if (mid * mid > 2)
                h = mid;
            else
                l = mid;
        }
        String s = l + "";
        return Double.parseDouble(s.substring(0, 12));
    }
}


